package com.yang.service;

import com.yang.dao.ManagerDao;
import com.yang.pojo.Manager;
import com.yang.utils.MD5Utils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 根据管理员输入的登录账号和密码验证登录是否成功
 */
public class ManagerService {
    ManagerDao managerDao = new ManagerDao();
    public Manager checkLogin(String loginName,String loginPwd) throws Exception {

        //获得数据库实体对象
        Manager manager = managerDao.selectManagerByLoginName(loginName);
        if(manager != null){
            //如果根据用户名查询得到对象，就判断密码是不是正确的
            //加密
            String pwd = MD5Utils.md5JiaMi(loginPwd);

            if(!pwd.equals(manager.getLoginPwd())){
                return null;
            }
        }
        return manager;
    }

    //调用dao查询所有的管理员
    public ArrayList<Manager> findManagerList() throws SQLException {
        ArrayList<Manager> managers = managerDao.selectManager();
        return managers;
    }

    //根据roleId查询管理员信息
    public ArrayList<Manager> findManagerByRoleId(int roleId) throws SQLException {
        ManagerDao managerDao = new ManagerDao();
        ArrayList<Manager> managers = new ArrayList<>();
        List<String> mgrIds = managerDao.selectMgrIdByRoleId(roleId);
        for (int i = 0; i < mgrIds.size(); i++) {
            String mgrId = mgrIds.get(i);
            Manager manager = managerDao.selectManagerByMgrId(mgrId);
            managers.add(manager);
        }
        return managers;
    }
}
